// 处理ajax返回的结果
function handleRes(res,code)
{
    if(code == 'parent')
    {
        if(res.code == 0)
        {
            layer.msg(res.msg,{icon:1,time:2000},function()
            {
                bind();
                layer.closeAll();
            })
        }
        else if(res.code == 1){
            layer.alert(res.msg,{icon:2})
        }
    }
    else{
        if(res.code == 0)
        {
            parent.layer.msg(res.msg,{icon:1,time:2000},function()
            {
                parent.bind();
                parent.layer.closeAll();
            })
        }
        else if(res.code == 1){
            parent.layer.alert(res.msg,{icon:2})
        }
    }
}

//上传文件

function upload(result,look='no')
{
    upload_file(result,'管理附件',20,'picture_mark',look);
}

function upload_file(attachment,title,size,input_name,isLook)
{
    if(!attachment){
        attachment = guid();
        $('input[name='+input_name+']').val(attachment);
    }
    var rel = attachment;
    if(isLook != 'yes'){
        isLook = 'no';
    }

    uploadView(rel,title,size,isLook);
}

function uploadView(rel,title,size,isLook)
{

    let  content = '/self/upload/index?rel=' + rel +'&is_look=' + isLook+'&size='+size+'&title='+title;
    layer.open({
        title: title,
        type: 2,
        area: ['40%', '70%'],
        fix: true,
        maxmin: false,
        content: content,
        yes:function (ee) {
            console.log(ee);
        }
    });
}


/*
    * 获取选中的那条数据
    * callback 回调函数
    * field    状态字段
    * value    状态值
    * 如果选中的数据,字段的值不等于value 则属于不能被操作的状态
    */
function getAllSelectOne(callback = function(){},field = 'id', value = '-1')
{
    let rows = $("#table").bootstrapTable('getAllSelections');
    if(rows.length == 0)
    {
        layer.alert("当前未选择数据,无法操作!",{icon:2});
        return;
    }

    if(rows.length > 1)
    {
        layer.alert("只能选择一条数据,请检查!",{icon:2});
        return;
    }

    let ids = [];
    for(let k in rows)
    {
        if(field !== 'id' && value !== '-1' && rows[k][field] != value)
        {
            layer.alert(`第 ${k + 1} 行数据无法进行此操作,请检查!`,{icon:2});
            return;
        }
        ids.push(rows[k]['id']);
    }
    callback(ids);
    return ids;
}

//获取多条
function getAllSelectMore(callback = function(){},field = 'id', value = '-1')
{
    let rows = $("#table").bootstrapTable('getAllSelections');
    if(rows.length == 0)
    {
        layer.alert("当前未选择数据,无法操作!",{icon:2});
        return;
    }

    let ids = [];
    for(let k in rows)
    {

        if(field !== 'id' && value !== '-1' && rows[k][field] != value)
        {
            layer.alert(`第 ${k + 1} 行数据无法进行此操作,请检查!`,{icon:2});
            return;
        }
        ids.push(rows[k]['id']);
    }
    callback(ids);
    return ids;
}

//传值给后台更改状态
function changeOneField(ids,status = 0,field = 'status')
{
    layer.confirm("您确定要进行此操作吗?",function()
    {
        frame.ajax({
            type:'post',
            url:'changeOneField',
            data:{ids:ids,status:status,field:field}
        }).then(function(res)
        {
            handleRes(res,'parent');
        })
    })
}

$(function()
{
    const tokenUser = localStorage.getItem('tokenUser');
    $.ajax({
        type:'post',
        url:'/self/extend/verificationToken',
        data:{tokenUser:tokenUser}
    }).then(function(res)
    {
        if(res.code == 403)
        {
            layer.alert(res.msg,function()
            {
                parent.window.location.href='/bus/index/index';
            })
        }
    })

})

function disable()
{
    var rows = $("#table").bootstrapTable("getAllSelections");

    if(rows.length == 0){
        ptshow({code:1,text:"请至少勾选一行数据"});
        return;
    }
    layer.confirm('确定要启/禁用当前数据吗?',function()
    {

        let ids = [];
        for(let k of rows)
        {
            ids.push(k['id']);
        }
        frame.ajax({
            type:'post',
            url:'disable',
            data:{ids:ids}
        }).then(function(res)
        {
            handleRes(res,'parent');
        })
    })
}
// 判断当前值是否在数组中
function in_array(value,array)
{
    for(var k in array)
    {
        if(array[k] == value) return k;
    }
    return -1;
}


function bannerImg(value,row,col)
{
    if(!value) return
    value = value.substr(1,value.length -2);
    let arrayV = value.split(',');
    let html = `<div class="swiper-container" style="z-index:888888">
                                <div class="swiper-wrapper">`;
    for(let k of arrayV)
    {
        var suffix = k.substring(k.lastIndexOf(".")+1).toLocaleLowerCase();//后缀名
        suffix = suffix.substring(0,suffix.length-1);
        k = k.substr(1,  k.length - 2);
        if(in_array(suffix,['png','jpg','jpeg','gif']) != -1) html +=`<div class="swiper-slide"><img src="${k}" alt=""></div>`;
        //if(in_array(suffix,['mp4']) != -1) html +=`<div class="swiper-slide"><video style="width:210px;"  src="${k}" controls="controls /></div>`;
        //if(in_array(suffix,['mp3']) != -1) html +=`<div class="swiper-slide"><audio style="width:210px;"  src="${k}" controls="controls /></div>`;
    }
    html += `</div>
                    </div>`;
    return html
}

function statusShow(value,row,col)
{
    return value == 0 ? '正常' : '禁用';
}


// 链接到音/视频
function linkVideo(that)
{
    let value = that.data('value');

    value = value.substr(1,value.length -2);
    let arrayV = value.split(',');

    let html = '<div >';
    for(let k of arrayV)
    {
        var suffix = k.substring(k.lastIndexOf(".")+1).toLocaleLowerCase();//后缀名
        suffix = suffix.substring(0,suffix.length-1);
        k = k.substr(1,  k.length - 2);
        if(in_array(suffix,['png','jpg','jpeg','gif']) != -1){
            html +=`
                    <div class="mt-2 ml-2 pull-left" style="width:300px;height:200px;border: 1px solid #e5e5e5;display: flex;align-items: center;justify-content: center;">
                       <a href="${k}" target="_blank" >
                          <img src="${k}" style="width:98%;height:98%;" object-fit: cover; />
                       </a>
                    </div>`;
        }
        if(in_array(suffix,['mp4']) != -1) {
            html +=`<div class="ml-2 pull-left mt-2" style="width:300px;height:200px;border: 1px solid #e5e5e5;display: flex;align-items: center;justify-content: center;">
                            <video src="${k}" style="width:98%;height:98%;" controls="controls" >
                            </video>
                        </div>`;
        }
        if(in_array(suffix,['mp3']) != -1) html +=`<div class=" ml-2 pull-left mt-2" style="width:300px;height:200px;border: 1px solid #e5e5e5;display: flex;align-items: center;justify-content: center;" ><audio src="${k}" style="width:100%" controls="controls" ></audio></div>`;
    }
    html += '</div>';
    layer.open({
        title:'多媒体',
        type: 1,
        anim: 2,
        shadeClose: true, //开启遮罩关闭
        content: html,
        area:['625px','70%']
    })
}




